<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="基本思路
将Hexo生成的博客静态文件利用git上传到VPS中的git仓库中
服务器端git仓库收到git push 之后，通过git hook 触发 一段shell 脚本
shell脚本将git push过来的网页静态文件复制到已经配置好nginx的文件目录下
通过nginx转发，则可以正常访问博客

git安装配置###1. 安装git12# root用户下yum install git
##">
<meta property="og:type" content="article">
<meta property="og:title" content="通过Git Hook 部署 Hexo 至VPS空间">
<meta property="og:url" content="http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/index.html">
<meta property="og:site_name" content="enomine">
<meta property="og:description" content="基本思路
将Hexo生成的博客静态文件利用git上传到VPS中的git仓库中
服务器端git仓库收到git push 之后，通过git hook 触发 一段shell 脚本
shell脚本将git push过来的网页静态文件复制到已经配置好nginx的文件目录下
通过nginx转发，则可以正常访问博客

git安装配置###1. 安装git12# root用户下yum install git
##">
<meta property="og:updated_time" content="2016-12-13T05:22:38.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="通过Git Hook 部署 Hexo 至VPS空间">
<meta name="twitter:description" content="基本思路
将Hexo生成的博客静态文件利用git上传到VPS中的git仓库中
服务器端git仓库收到git push 之后，通过git hook 触发 一段shell 脚本
shell脚本将git push过来的网页静态文件复制到已经配置好nginx的文件目录下
通过nginx转发，则可以正常访问博客

git安装配置###1. 安装git12# root用户下yum install git
##">
    
    
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
    
    <!-- title -->
    <title>通过Git Hook 部署 Hexo 至VPS空间</title>
    <!-- styles -->
    <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/lib/meslo-LG/styles.css">
    <link rel="stylesheet" href="/lib/justified-gallery/justifiedGallery.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <!-- rss -->
    
    
    <!-- jquery -->
    <script src="/lib/jquery/jquery.min.js"></script>
</head>

<body>
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fa fa-bars fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="http://github.com/enominecn">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
      <ul>
        
        <li><a class="icon" href="/2017/04/17/git中文乱码/"><i class="fa fa-chevron-left" aria-hidden="true" onmouseover='$("#i-prev").toggle();' onmouseout='$("#i-prev").toggle();'></i></a></li>
        
        
        <li><a class="icon" href="/2016/12/11/deploy-hexo-blog-to-vps-by-git-hook/"><i class="fa fa-chevron-right" aria-hidden="true" onmouseover='$("#i-next").toggle();' onmouseout='$("#i-next").toggle();'></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa fa-chevron-up" aria-hidden="true" onmouseover='$("#i-top").toggle();' onmouseout='$("#i-top").toggle();'></i></a></li>
        <li><a class="icon" href="#"><i class="fa fa-share-alt" aria-hidden="true" onmouseover='$("#i-share").toggle();' onmouseout='$("#i-share").toggle();' onclick='$("#share").toggle();return false;'></i></a></li>
      </ul>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
        <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://twitter.com/share?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&text=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&title=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-linkedin" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&is_video=false&description=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-pinterest" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="mailto:?subject=通过Git Hook 部署 Hexo 至VPS空间&body=Check out this article: http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/"><i class="fa fa-envelope" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://getpocket.com/save?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&title=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-get-pocket" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://reddit.com/submit?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&title=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-reddit" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&title=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-stumbleupon" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://digg.com/submit?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&title=通过Git Hook 部署 Hexo 至VPS空间"><i class="fa fa-digg" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://enomineCN.github.io/2016/12/11/通过GitHook部署Hexo至VPS空间/&name=通过Git Hook 部署 Hexo 至VPS空间&description="><i class="fa fa-tumblr" aria-hidden="true"></i></a></li>
      </ul>
    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#基本思路"><span class="toc-number">1.</span> <span class="toc-text">基本思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#git安装配置"><span class="toc-number">2.</span> <span class="toc-text">git安装配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#nginx安装配置"><span class="toc-number">3.</span> <span class="toc-text">nginx安装配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#安装"><span class="toc-number">3.1.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#启动并设置开机启动"><span class="toc-number">3.2.</span> <span class="toc-text">启动并设置开机启动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#打开防火墙"><span class="toc-number">3.3.</span> <span class="toc-text">打开防火墙</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#配置nginx"><span class="toc-number">3.4.</span> <span class="toc-text">配置nginx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#重启nginx"><span class="toc-number">3.5.</span> <span class="toc-text">重启nginx</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#git-hook-配置"><span class="toc-number">4.</span> <span class="toc-text">git hook 配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置-Hexo-的部署信息"><span class="toc-number">5.</span> <span class="toc-text">配置 Hexo 的部署信息</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index width mx-auto px2 my4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        通过Git Hook 部署 Hexo 至VPS空间
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">enomine</span>
      </span>
      
    <div class="postdate">
        <time datetime="2016-12-11T08:23:26.000Z" itemprop="datePublished">2016-12-11</time>
    </div>


      
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/VPS/">VPS</a>, <a class="tag-link" href="/tags/git-hook/">git hook</a>, <a class="tag-link" href="/tags/hexo/">hexo</a>, <a class="tag-link" href="/tags/nginx/">nginx</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="基本思路"><a href="#基本思路" class="headerlink" title="基本思路"></a>基本思路</h2><ul>
<li>将Hexo生成的博客静态文件利用git上传到VPS中的git仓库中</li>
<li>服务器端git仓库收到git push 之后，通过git hook 触发 一段shell 脚本</li>
<li>shell脚本将git push过来的网页静态文件复制到已经配置好nginx的文件目录下</li>
<li>通过nginx转发，则可以正常访问博客</li>
</ul>
<h2 id="git安装配置"><a href="#git安装配置" class="headerlink" title="git安装配置"></a>git安装配置</h2><p>###1. 安装git<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># root用户下</span></div><div class="line">yum install git</div></pre></td></tr></table></figure></p>
<p>###2.新建git用户<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#root用户下</span></div><div class="line"><span class="comment">#添加用户</span></div><div class="line">adduser git</div><div class="line"><span class="comment"># 修改密码</span></div><div class="line">passwd git</div></pre></td></tr></table></figure></p>
<p>###3.配置git用户<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># git用户下</span></div><div class="line"><span class="comment"># 添加ssh公钥</span></div><div class="line">mkdir -p ~/.ssh</div><div class="line">vi ~/.ssh/authorized_keys</div><div class="line"><span class="comment"># 添加文件及目录权限</span></div><div class="line">chmod 700 ~/.ssh</div><div class="line">chmod 600 ~/.ssh/authorized_keys</div><div class="line"><span class="comment"># 禁用密码登录</span></div><div class="line">PasswordAuthentication no</div></pre></td></tr></table></figure></p>
<p>###4.建立git仓库<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># git用户下</span></div><div class="line"><span class="built_in">cd</span> ~</div><div class="line">mkdir blog.git</div><div class="line"><span class="built_in">cd</span> blog.git</div><div class="line">git init --bare</div></pre></td></tr></table></figure></p>
<h2 id="nginx安装配置"><a href="#nginx安装配置" class="headerlink" title="nginx安装配置"></a>nginx安装配置</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install nginx</div></pre></td></tr></table></figure>
<h3 id="启动并设置开机启动"><a href="#启动并设置开机启动" class="headerlink" title="启动并设置开机启动"></a>启动并设置开机启动</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">systemctl start nginx.service</div><div class="line">systemctl <span class="built_in">enable</span> nginx.service</div></pre></td></tr></table></figure>
<h3 id="打开防火墙"><a href="#打开防火墙" class="headerlink" title="打开防火墙"></a>打开防火墙</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">firewall-cmd --permanent --zone=public --add-service=http</div><div class="line">firewall-cmd --permanent --zone=public --add-service=https</div><div class="line">firewall-cmd --reload</div></pre></td></tr></table></figure>
<h3 id="配置nginx"><a href="#配置nginx" class="headerlink" title="配置nginx"></a>配置nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /etc/nginx/conf.d/blog.conf</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># blog.conf</span></div><div class="line">server &#123;</div><div class="line">  root /home/wwwroot/hexo; <span class="comment"># 网站目录</span></div><div class="line">  index index.html index.htm;</div><div class="line">  server_name www.***.com *.***.com ; <span class="comment"># 域名 或 IP</span></div><div class="line">  location / &#123;</div><div class="line">    try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ /index.html;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="重启nginx"><a href="#重启nginx" class="headerlink" title="重启nginx"></a>重启nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nginx <span class="_">-s</span> reload</div></pre></td></tr></table></figure>
<h2 id="git-hook-配置"><a href="#git-hook-配置" class="headerlink" title="git hook 配置"></a>git hook 配置</h2><p>VPS的 hexo网站目录为 <code>/home/wwwroot/hexo/</code></p>
<p>这个目录要给 git 用户访问，<code>hexo.git</code> 仓库收到提交后，git 用户要把提交的内容再 checkout 到 <code>/home/wwwroot/hexo/</code> 目录，所以要把这个目录的所有权交给 git 用户：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"># root 下</div><div class="line">chown git:git /home/wwwroot/hexo/</div></pre></td></tr></table></figure>
<p>建立一个仓库内容转移的缓存目录：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir ~/tmp/hexo_tmp</div></pre></td></tr></table></figure>
<p>然后处理 hexo.git 提交的事件，自动更新内容到 hexo网站目录。在 git 用户下执行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">cd ~/hexo.git/hooks</div><div class="line">touch post-receive</div><div class="line">vi post-receive</div></pre></td></tr></table></figure>
<p>编辑这个 post-receive 文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">#!/bin/bash -l</div><div class="line">GIT_REPO=/home/git/hexo.git</div><div class="line">TMP_GIT_CLONE=/home/git/tmp/hexo_tmp</div><div class="line">PUBLIC_WWW=/home/wwwroot/hexo</div><div class="line">rm -rf $&#123;TMP_GIT_CLONE&#125;</div><div class="line">git clone $GIT_REPO $TMP_GIT_CLONE</div><div class="line">rm -rf $&#123;PUBLIC_WWW&#125;/*</div><div class="line">cp -rf $&#123;TMP_GIT_CLONE&#125;/* $&#123;PUBLIC_WWW&#125;</div></pre></td></tr></table></figure>
<p>这个脚本可以实现完全更新，即先删除原网站内所有内容，再拷贝新仓库内容到网站目录，可以避免一些不必要的问题。</p>
<p>还要给脚本加上执行权限：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chmod +x post-receive</div></pre></td></tr></table></figure>
<h2 id="配置-Hexo-的部署信息"><a href="#配置-Hexo-的部署信息" class="headerlink" title="配置 Hexo 的部署信息"></a>配置 Hexo 的部署信息</h2><p>打开 <code>_config.yml</code> 文件，找到 <strong>deploy</strong> 字段，修改如下：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="attr">deploy:</span> </div><div class="line"><span class="attr">- type:</span> git</div><div class="line"><span class="attr">  repo:</span> git@**.**.**.**:hexo.git</div><div class="line"><span class="attr">  branch:</span> master</div><div class="line"><span class="attr">- type:</span> git</div><div class="line"><span class="attr">  repo:</span> git@github.com:enominecn/enominecn.github.io.git </div><div class="line"><span class="attr">  branch:</span> master</div></pre></td></tr></table></figure>
  </div>
</article>



      </div>
    <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2017 enomine
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="http://github.com/enominecn">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    <script src="/lib/justified-gallery/jquery.justifiedGallery.min.js"></script>
<script src="/js/main.js"></script>
<!-- Google Analytics -->

    <script type="text/javascript">
        (function(i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function() {
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
        ga('create', 'UA-86660611-1', 'auto');
        ga('send', 'pageview');
    </script>

<!-- Disqus Comments -->


</body>
</html>
